jira_php

Searching for issues

We can use the search API available at http://example.com/rest/api/2/search with the JQL “created > -1d”.

<?php

//pull in login credentials and CURL access function
 require_once("utils.php");

//create a payload that we can then pass to JIRA with JSON
$jql = array(
	'jql' => 'created > -1d'
);

/*define a function that calls the right REST API
We convert the array to JSON inside of the function. */
function search_issue($issue) {
	return get_from('search', $issue);
}

//call JIRA.
$result = search_issue($jql);

//check for errors
if (property_exists($result, 'errors')) {
	echo "Error(s) searching for issues:n";
	var_dump($result);
} else {
	//print out the issue keys and summaries
	echo "Here are the issue keys and summariesn";
	foreach ($result->issues as &$issue) {
    	echo($issue->key . "  " . $issue->fields->summary . "n");
	}
}

?>

Let’s take a look at the get_from function to see how PHP is interacting with JIRA.

function get_from($resource, $data) {
	//convert array to JSON string
	$jdata = json_encode($data);
	$ch = curl_init();
	//configure CURL
	curl_setopt_array($ch, array(
		CURLOPT_URL => JIRA_URL . '/rest/api/latest/' . $resource,
		CURLOPT_USERPWD => USERNAME . ':' . PASSWORD,
		CURLOPT_POSTFIELDS => $jdata,
		CURLOPT_HTTPHEADER => array('Content-type: application/json'),
		CURLOPT_RETURNTRANSFER => true
	));
	$result = curl_exec($ch);
	curl_close($ch);
	//convert JSON data back to PHP array
	return json_decode($result);
}

Creating Issues

<?php
 require_once("utils.php");

$new_issue = array(
	'fields' => array(
		'project' => array('key' => 'TIS'),
		'summary' => 'Test via REST',
		'description' => 'Description of issue goes here.',
		'priority' => array('name' => 'Blocker'),
		'issuetype' => array('name' => 'Task'),
		'labels' => array('a','b')
	)
);

function create_issue($issue) {
	return post_to('issue', $issue);
}

$result = create_issue($new_issue);
if (property_exists($result, 'errors')) {
	echo "Error(s) creating issue:n";
	var_dump($result);
} else {
	echo "New issue created at " . JIRA_URL ."/browse/{$result->key}n";
}
?>
function post_to($resource, $data) {
	$jdata = json_encode($data);
	$ch = curl_init();
	curl_setopt_array($ch, array(
		CURLOPT_POST => 1,
		CURLOPT_URL => JIRA_URL . '/rest/api/latest/' . $resource,
		CURLOPT_USERPWD => USERNAME . ':' . PASSWORD,
		CURLOPT_POSTFIELDS => $jdata,
		CURLOPT_HTTPHEADER => array('Content-type: application/json'),
		CURLOPT_RETURNTRANSFER => true
	));
	$result = curl_exec($ch);
	curl_close($ch);
	return json_decode($result);
}

Editing issues

<?php
 require_once("utils.php");

$key = 'TIS-71';
$fields = array(
	'fields' => array(
		'summary' => "Update to issue 71"
	)
);

function edit_issue($key, $fields) {
	return put_to('issue/'.$key, $fields);
}

$result = edit_issue($key, $fields);
if ($result != NULL) {
	echo "Error(s) creating issue:n";
	var_dump($result);
} else {
	echo "Edits complete. Issue can be viewed at " . JIRA_URL ."/browse/{$key}n";
}
?>
function put_to($resource, $data) {
	$jdata = json_encode($data);
	$ch = curl_init();
	curl_setopt_array($ch, array(
		CURLOPT_CUSTOMREQUEST=>"PUT",
		CURLOPT_URL => JIRA_URL . '/rest/api/latest/' . $resource,
		CURLOPT_USERPWD => USERNAME . ':' . PASSWORD,
		CURLOPT_POSTFIELDS => $jdata,
		CURLOPT_HTTPHEADER => array(
			'Accept: application/json',
			'Content-Type: application/json'
			),
		CURLOPT_RETURNTRANSFER => true
	));
	$result = curl_exec($ch);
	curl_close($ch);
	return json_decode($result);
}

Full source can be had here.

Posted by:.

3 replies on “PHP Scripts with JIRA REST API

Leave a comment